<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" style="width: 200px;" class="filter-item" placeholder="请输入消防设施名称" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>
    <div class="fontColor">
      <el-tabs type="border-card">
        <el-tab-pane label="所有设施">
          <AssetList ref="assetList"/>
        </el-tab-pane>
        <el-tab-pane label="监控设施">
          <AssignmentList/>
        </el-tab-pane>
      </el-tabs>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="消防设施" prop="assetcategoryid">
          <el-select v-model="temp.assetcategoryid" placeholder="消防设施类型">
            <el-option v-for="item in stOptions" :key="item.categoryid" :label="item.categoryname" :value="item.categoryid"/>
          </el-select>
        </el-form-item>
        <el-form-item label="位置信息" prop="alocation">
          <el-input v-model="temp.alocation" placeholder="示例：高新南四道R1-A栋现代大厦二层" />
        </el-form-item>
        <el-form-item label="站点ID" prop="siteid">
          <el-input :disabled="true" v-model="pub_siteid"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AssignmentList from '@/views/fire/asset/assignmentlist'
import AssetList from '@/views/fire/asset/assetlist'
import { addAsset, assetCategory } from '@/api/fire/asset/asset'
import { isNotNull, length } from '@/utils/validate'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'AssetIndex',
  directives: {
    waves
  },
  components: {
    AssignmentList,
    AssetList
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      total: null,
      pub_siteid: this.$store.state.asset.siteid,
      listLoading: true,
      stOptions: [],
      listQuery: {
        page: 1,
        pasesize: 10,
        name: ''
      },
      showReviewer: false,
      temp: {
        assetcategoryid: '',
        siteid: '',
        longitude: '',
        latitude: '',
        elevation: '',
        spotimage: '',
        arange: '',
        alocation: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        create: '创建'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        assetcategoryid: isNotNull('请选择消防设施类型'),
        alocation: isNotNull('请输入位置信息').concat(length(6, 1024))
      },
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false
    }
  },
  created() {
    this.getAssetCategory()
  },
  methods: {
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    getAssetCategory() {
      assetCategory().then(response => {
        console.log(response)
        this.stOptions = response.data.context.assetcategorylist
      })
    },
    resetTemp() {
      this.temp = {
        assetcategoryid: '',
        siteid: '',
        longitude: '',
        latitude: '',
        elevation: '',
        spotimage: '',
        arange: '',
        alocation: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleFilter() {
      console.log(this.listQuery.name)
      this.$refs.assetList.AssetSearch(this.listQuery.name)
      // this.queryMaintainorOptions()
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.siteid = this.$store.state.asset.siteid
          addAsset(this.temp).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.$refs.assetList.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" >
  /*可能影响到其他的tab*/
  .fontColor #tab-1 {
      color: #ff1313 !important;
    }

</style>
